package com.example.yigoumall.controller;

import com.example.yigoumall.entity.News;
import com.example.yigoumall.service.NewsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@RestController
@RequestMapping("/api/news")
public class NewsController {
    
    @Autowired
    private NewsService newsService;
    
    @GetMapping
    public List<News> getAllNews() {
        return newsService.findAll();
    }
    
    @GetMapping("/{id}")
    public News getNewsById(@PathVariable Integer id) {
        return newsService.findById(id);
    }
    
    @GetMapping("/search")
    public List<News> searchNews(@RequestParam String title) {
        return newsService.searchByTitle(title);
    }
    
    @PostMapping
    public News createNews(@RequestBody News news) {
        return newsService.save(news);
    }
    
    @PutMapping("/{id}")
    public News updateNews(@PathVariable Integer id, @RequestBody News news) {
        news.setId(id);
        return newsService.save(news);
    }
    
    @DeleteMapping("/{id}")
    public void deleteNews(@PathVariable Integer id) {
        newsService.deleteById(id);
    }
}